<?php

namespace app\common\model\user;

use app\common\model\store\User;
use app\common\model\tableField\TableFieldEnumModel;
use app\utils\business\BaseModel;
use app\utils\library\Mask;
use think\Exception;

class UserRealModel extends BaseModel
{
    protected $table = 'yoshop_user_real';
    
    // =========================  模型关联 定义区域 开始 =======================================
    public function user()
    {
        return $this->belongsTo(User::class,'user_id', 'user_id');
    }
    
    // =========================  模型关联 定义区域 结束 =======================================
    
    // ========================= get 获取器 定义区域 开始 =======================================
    
    public function getIdCardAttr($value,$data)
    {
        return Mask::createInstance()->idCard($value);
    }
    
    public function getOutlookExplainCnAttr($value,$data)
    {
        return $this->getFieldExplainCn('outlook', $data['outlook']);
    }
    
    public function getEducationExplainCnAttr($value,$data)
    {
        return $this->getFieldExplainCn('education', $data['education']);
    }
    
    public function getRetireStatusExplainCnAttr($value,$data)
    {
        return $this->getFieldExplainCn('retire_status', $data['retire_status']);
    }
    
    public function getCompanyCategoryIdExplainCnAttr($value,$data)
    {
        return $this->getFieldExplainCn('company_category_id', $data['company_category_id']);
    }
    
    // ========================= get 获取器 定义区域 结束 =======================================
    
    
    /**
     * 获取 字段的某个值的中文释义
     * @param string $fieldName 字段名
     * @param string|int $value 值
     * @return int|string
     */
    public function getFieldExplainCn(string $fieldName, $value) {
        return TableFieldEnumModel::getExplainCn('yoshop_user_real', $fieldName, $value);
    }
    
    /**
     * 获取 字段枚举 列表
     * @param string $fieldName
     * @return array
     */
    public function getTableFieldEnumList(string $fieldName): array
    {
        return TableFieldEnumModel::getTableFieldEnumList('yoshop_user_real', $fieldName);
    }
    
    /**
     * 获取 政治面貌 字段枚举 列表
     * @return array
     */
    public function getOutlookEnumList(): array
    {
        return $this->getTableFieldEnumList('outlook');
    }
    
    /**
     * 获取 文化程度 字段枚举 列表
     * @return array
     */
    public function getEducationEnumList(): array
    {
        return $this->getTableFieldEnumList('education');
    }
    
    /**
     * 获取 退休状态 字段枚举 列表
     * @return array
     */
    public function getRetireStatusEnumList(): array
    {
        return $this->getTableFieldEnumList('retire_status');
    }
    
    /**
     * 获取 单位类型 字段枚举 列表
     * @return array
     */
    public function getCompanyCategoryIdEnumList(): array
    {
        return $this->getTableFieldEnumList('company_category_id');
    }
    
    public function saveData($data)
    {
        if (empty($data['user_id'])) {
            throw new Exception('数据非法');
        }
        
        $userRealModel = $this->get($data['user_id']);
        if (empty($userRealModel)) {
            $userRealModel = new static();
            return $userRealModel->allowField(true)->isUpdate(false)->save($data);
        }
        $userRealModel->allowField(true)->isUpdate()->save($data);
        return $userRealModel;
    }
    
    public function getUserReal($id_card)
    {
        return $this->where('id_card', '=', $id_card)->find();
    }
}